package com.knightboy.smartshoes.myUi;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.knightboy.smartshoes.R;
import com.knightboy.smartshoes.coreUtil.SharedData;

import java.util.ArrayList;

/**
 * Created by knightBoy on 2015/7/16.
 */
public class TodayActivity extends Activity {
    private Button more_bt, share_bt, back_bt;
    private TextView today_step_tv, today_rate_tv, today_distance_tv,
                     today_hour1_tv,today_hour2_tv,today_hour3_tv,
                     today_minute1_tv,today_minute2_tv,today_minute3_tv,
                     today_calorie1_tv,today_calorie2_tv,today_calorie3_tv;
    //柱形图及其数据
    private BarChart mBarChart;
    private BarData mBarData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.taday_fuc);
        //初始化
        init();
        //显示今日健康数据
        showHealthData();
        //柱形图显示
        mBarData = getBarData(24, 100);
        showBarChart(mBarChart,mBarData);
    }

    /**
     * 初始化程序
     */
    private void init(){
        //获取控件对象
        today_step_tv = (TextView)findViewById(R.id.today_step_tv);
        today_rate_tv = (TextView)findViewById(R.id.today_rate_tv);
        today_distance_tv = (TextView)findViewById(R.id.today_distance_tv);
        today_hour1_tv = (TextView)findViewById(R.id.today_hour1_tv);
        today_hour2_tv = (TextView)findViewById(R.id.today_hour2_tv);
        today_hour3_tv = (TextView)findViewById(R.id.today_hour3_tv);
        today_minute1_tv = (TextView)findViewById(R.id.today_minute1_tv);
        today_minute2_tv = (TextView)findViewById(R.id.today_minute2_tv);
        today_minute3_tv = (TextView)findViewById(R.id.today_minute3_tv);
        today_calorie1_tv = (TextView)findViewById(R.id.today_calorie1_tv);
        today_calorie2_tv = (TextView)findViewById(R.id.today_calorie2_tv);
        today_calorie3_tv = (TextView)findViewById(R.id.today_calorie3_tv);
        more_bt = (Button)findViewById(R.id.more_bt);
        share_bt = (Button)findViewById(R.id.share_bt);
        back_bt = (Button)findViewById(R.id.back_bt);

        mBarChart = (BarChart)findViewById(R.id.step_barchart);

        //"最近七天"按钮添加监听器
        more_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(TodayActivity.this, RecentActivity.class);
                startActivity(intent);
            }
        });

        //分享按钮添加监听器
        share_bt.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //分享纯文本
                Intent intent=new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");                       // 纯文本
                intent.putExtra(Intent.EXTRA_SUBJECT, "Share");     // 消息标题
                intent.putExtra(Intent.EXTRA_TEXT, "今天我已走了"+SharedData.getStep()+"步。快来参与吧!"); //消息文本
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(Intent.createChooser(intent,"今日详情分享"));
            }
        });

        //为回退按钮添加监听器
        back_bt.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 显示健康数据
     */
    private void showHealthData(){
        //更新步数
        today_step_tv.setText(SharedData.getStep()+"");
        //更新比率
//        today_rate_tv.setText(SharedData.getRate() + "");
        //更新路程
        today_distance_tv.setText(SharedData.getDistance()/1000.0+"");
        //更新总活动时间小时数
        today_hour1_tv.setText(SharedData.getActiveTime()/60 + "");
        //更新最长活动时间小时数
        today_hour2_tv.setText(SharedData.getLongestActiveTime()/60 + "");
        //更新最长空闲时间小时数
        today_hour3_tv.setText(SharedData.getLongestRestTime()/60 + "");
        //更新总活动时间分钟数
        today_minute1_tv.setText((SharedData.getActiveTime() - SharedData.getActiveTime()/60) + "");
        //更新最长活动时间分钟数
        today_minute2_tv.setText((SharedData.getLongestActiveTime() - SharedData.getLongestActiveTime()/60) + "");
        //更新最长空闲时间分钟数
        today_minute3_tv.setText((SharedData.getLongestRestTime() - SharedData.getLongestRestTime()/60) + "");
        //更新总卡路里数
        today_calorie1_tv.setText((SharedData.getActiveCalorie() + SharedData.getRestCalorie()) + "" );
        //更新活动卡路里数
        today_calorie2_tv.setText(SharedData.getActiveCalorie() + "");
        //更新休息卡路里数
        today_calorie3_tv.setText(SharedData.getRestCalorie() + "");
    }

    /**
     * 显示柱形图
     * @param barChart 柱形图对象
     * @param barData 柱形图数据
     */
    private void showBarChart(BarChart barChart, BarData barData) {
        barChart.setDrawBorders(false);  ////是否在折线图上添加边框

        barChart.setDescription("");// 数据描述

        // 如果没有数据的时候，会显示这个，类似ListView的EmptyView
        barChart.setNoDataTextDescription("You need to provide data for the chart.");

        barChart.setDrawGridBackground(false); // 是否显示表格颜色
        barChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF); // 表格的的颜色，在这里是是给颜色设置一个透明度

        barChart.setTouchEnabled(true); // 设置是否可以触摸

        barChart.setDragEnabled(true);// 是否可以拖拽
        barChart.setScaleEnabled(true);// 是否可以缩放

        barChart.setPinchZoom(false);//

//      barChart.setBackgroundColor();// 设置背景

        barChart.setDrawBarShadow(true);

        barChart.setData(barData); // 设置数据

        Legend mLegend = barChart.getLegend(); // 设置比例图标示

        mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式
        mLegend.setFormSize(6f);// 字体
        mLegend.setTextColor(Color.BLACK);// 颜色

//      X轴设定
//      XAxis xAxis = barChart.getXAxis();
//      xAxis.setPosition(XAxisPosition.BOTTOM);

        barChart.animateX(2500); // 立即执行的动画,x轴
    }

    /**
     * 获得柱形图数据
     * @param count 数据个数
     * @param range 数据最大值
     * @return
     */
    private BarData getBarData(int count, float range) {
        ArrayList<String> xValues = new ArrayList<String>();
//        String[] week = new String[]{"周一","周二","周三","周四","周五","周六","周日"};
        for (int i = 0; i < count; i++) {
            xValues.add(i+1 + ":00");
        }

        ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();

        for (int i = 0; i < count; i++) {
            float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
            yValues.add(new BarEntry(value, i));
        }

        // y轴的数据集合
        BarDataSet barDataSet = new BarDataSet(yValues, "今天行走步数分布图(单位:百步)");

//        barDataSet.setColor(Color.rgb(114, 188, 223));
        barDataSet.setColor(Color.rgb(228, 103, 18));

        ArrayList<BarDataSet> barDataSets = new ArrayList<BarDataSet>();
        barDataSets.add(barDataSet); // add the datasets

        BarData barData = new BarData(xValues, barDataSets);

        return barData;
    }

}
